package edu.uci.ics.ai.maze.controllers;

import edu.uci.ics.ai.maze.io.SimpleFileParser;
import edu.uci.ics.ai.maze.models.GameModel;
import edu.uci.ics.ai.maze.models.SearchAlgorithm;
import edu.uci.ics.ai.maze.views.GameFrame;
import edu.uci.ics.ai.maze.views.VisibleVertexDialogModel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class Game {

  private final GameModel model;
  private final SearchEngine searchEngine;
  private VisibleVertexDialogModel visVertexDialogModel;
  private GameFrame gameFrame;

  public Game() {
    model = new GameModel();
    searchEngine = new SearchEngine(model);
  }

  /**
   * Displays the user interface
   */
  public void display() {
    visVertexDialogModel = new VisibleVertexDialogModel(this.getModel());
    gameFrame = new GameFrame(this);

    getWindow().setVisible(true);
  }

  /**
   * Loads the maze in the specified file.
   * 
   * @param filename the maze file
   */
  public void loadFile(String filename) throws FileNotFoundException, IOException {
    getModel().setMaze(SimpleFileParser.parseFile(filename));
  }

  /**
   * Registers the search algorithms for the game
   * @param algorithms
   */
  public void registerAlgorithms(List<SearchAlgorithm> algorithms) {
    for (SearchAlgorithm algorithm : algorithms) {
      getSearchEngine().registerAlgorithm(algorithm);
    }
  }

  /**
   * Exits the game
   */
  public void close() {
    System.exit(0);
  }

  public GameFrame getWindow() {
    return gameFrame;
  }

  public GameModel getModel() {
    return model;
  }

  public SearchEngine getSearchEngine() {
    return searchEngine;
  }

  public VisibleVertexDialogModel getDialogModel() {
    return visVertexDialogModel;
  }
}
